package com.gyms.view.bottomview;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.view.View;

import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.R;
import com.zhy.autolayout.AutoLinearLayout;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * 地图选择框
 */
public class MapSelectDialog extends BaseBottomDialog {
    private AutoLinearLayout llayoutBaidu, llayoutGaode, mSure;
    private Context context;
    private String lat, log;

    public MapSelectDialog(Context context, int animationStyles, String lat, String log) {
        super(context, R.style.BottomViewTheme_Defalut);
        this.lat = lat;
        this.log = log;
        this.context = context;
        animationStyle = animationStyles;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_map;
    }


    @Override
    protected void initView() {
        llayoutBaidu = (AutoLinearLayout) findViewById(R.id.llayout_baidu);
        llayoutGaode = (AutoLinearLayout) findViewById(R.id.llayout_gaode);
        mSure = (AutoLinearLayout) findViewById(R.id.llayout_cancle);
        llayoutBaidu.setOnClickListener(onClickListener);
        llayoutGaode.setOnClickListener(onClickListener);
        mSure.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.llayout_baidu:
                    toBaiduMap();
                    dismiss();
                    break;
                case R.id.llayout_gaode:
                    toGaodeMap();
                    dismiss();
                    break;
                case R.id.llayout_cancle:
                    dismiss();
                    break;
            }
        }
    };

    private void toBaiduMap() {
        Intent intent;
        if (isAvilible(context, "com.baidu.BaiduMap")) {//传入指定应用包名

            try {
//                          intent = Intent.getIntent("intent://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving®ion=西安&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                intent = Intent.getIntent("intent://map/direction?" +
                        //"origin=latlng:"+"34.264642646862,108.95108518068&" +   //起点  此处不传值默认选择当前位置
                        "destination=latlng:" + lat + "," + log + "|name:我的目的地" +        //终点
                        "&mode=driving&" +          //导航路线方式
                        "region=杭州" +           //
                        "&src=呼啦#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                context.startActivity(intent); //启动调用
            } catch (URISyntaxException e) {
                Log.e("intent", e.getMessage());
            }
        } else {//未安装
            //market为路径，id为包名
            //显示手机上所有的market商店
            ToastUtil.showToast(context, "您尚未安装百度地图");
//            Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
//            intent = new Intent(Intent.ACTION_VIEW, uri);
//            context.startActivity(intent);
        }
    }

    private void toGaodeMap() {
        Intent intent;
        if (isAvilible(context, "com.autonavi.minimap")) {
            try {
//                intent = Intent.getIntent("androidamap://navi?sourceApplication=呼啦&poiname=我的目的地&lat=" + lat + "," + log + "&dev=0");
                intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                //将功能Scheme以URI的方式传入data
                Uri uri = Uri.parse("androidamap://navi?sourceApplication=呼啦&poiname=我的目的地&lat="+lat+"+&lon="+log+"&dev=1&style=2");
                intent.setData(uri);
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            ToastUtil.showToast(context, "您尚未安装高德地图");
//            Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
//            intent = new Intent(Intent.ACTION_VIEW, uri);
//            context.startActivity(intent);
        }
    }

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName：应用包名
     * @return
     */
    public static boolean isAvilible(Context context, String packageName) {
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

}
